package hsl.study.controller;

import com.alibaba.cloud.ai.advisor.DocumentRetrievalAdvisor;
import com.alibaba.cloud.ai.dashscope.api.DashScopeApi;
import com.alibaba.cloud.ai.dashscope.rag.DashScopeDocumentRetriever;
import com.alibaba.cloud.ai.dashscope.rag.DashScopeDocumentRetrieverOptions;
import jakarta.annotation.Resource;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

@RestController
@RequestMapping("/bailian")
public class BailianRagController {
    @Resource
    private DashScopeApi dashScopeApi;
    @Resource
    private ChatClient chatClient;

    @GetMapping("/rag/chat")
    public Flux<String> chat(@RequestParam(name = "question", defaultValue = "0001对应的名字") String question) {
        //1 RetrieverOptions参数配置
        DashScopeDocumentRetrieverOptions documentRetrieverOptions = DashScopeDocumentRetrieverOptions.builder()
                .withIndexName("java知识库")
                .build();

        //2. 百炼平台RAG知识库构建器
        DashScopeDocumentRetriever dashScopeDocumentRetriever = new DashScopeDocumentRetriever(dashScopeApi, documentRetrieverOptions);

        return chatClient.prompt()
                .user(question)
                .advisors(new DocumentRetrievalAdvisor(dashScopeDocumentRetriever))
                .stream()
                .content();
    }
}
